package com.gjy.util.network;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * @author gjy
 * @version 1.0
 * @since 2024-10-13 10:29:36
 */
public class PortScan {

    private static final Logger log = LoggerFactory.getLogger(PortScan.class);

    public static void main(String[] args) {
        String host = "http://x11aa913jfjoh69.com/"; // 替换为要扫描的域名或IP地址
        int startPort = 1;           // 起始端口
        int endPort = 65535;         // 结束端口

        for (int port = startPort; port <= endPort; port++) {
            scanPort(host, port);
        }
    }

    private static void scanPort(String host, int port) {
        try (Socket socket = new Socket()) {
            // 设置连接超时时间，避免长时间等待
            socket.connect(new InetSocketAddress(host, port), 2000);
            log.info("port={}", port);
        } catch (IOException e) {
            // 端口未开启, 也可以选择不输出任何内容
        }
    }

}
